package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AwPlanConfig;
import com.ruoyi.system.domain.AwStartPlan;
import com.ruoyi.system.manager.StartPlanManager;
import com.ruoyi.system.mapper.AwPlanConfigMapper;
import com.ruoyi.system.service.IAwPlanConfigService;
import com.ruoyi.system.service.IAwStartPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 启动计划配置Service业务层处理
 *
 * @author anxinr
 * @date 2023-08-11
 */
@Service
public class AwPlanConfigServiceImpl extends ServiceImpl<AwPlanConfigMapper ,AwPlanConfig> implements IAwPlanConfigService
{
    @Autowired
    private AwPlanConfigMapper awPlanConfigMapper;

    @Autowired
    private IAwStartPlanService awStartPlanService ;

    /**
     * 查询启动计划配置
     *
     * @param id 启动计划配置主键
     * @return 启动计划配置
     */
    @Override
    public AwPlanConfig selectAwPlanConfigById(Long id)
    {
        return awPlanConfigMapper.selectAwPlanConfigById(id);
    }

    /**
     * 查询启动计划配置列表
     *
     * @param awPlanConfig 启动计划配置
     * @return 启动计划配置
     */
    @Override
    public List<AwPlanConfig> selectAwPlanConfigList(AwPlanConfig awPlanConfig)
    {
        return awPlanConfigMapper.selectAwPlanConfigList(awPlanConfig);
    }

    /**
     * 新增启动计划配置
     *
     * @param awPlanConfig 启动计划配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertAwPlanConfig(AwPlanConfig awPlanConfig) throws IOException {
        if (!Objects.isNull(awPlanConfig)) {
            if (!Objects.isNull(awPlanConfig.getStartPlanId())) {
                AwStartPlan plan = awStartPlanService.getById(awPlanConfig.getStartPlanId());
                if (plan.getIsDefault().equals(1)) {
                    StartPlanManager.appendConfigToConfigFile(awPlanConfig.getConfig());
                }
            }else {
                throw new ServiceException("启动计划编号不能为空！") ;
            }
        }else {
            throw new ServiceException("启动配置不能为空！") ;
        }
        awPlanConfig.setCreateTime(DateUtils.getNowDate());
        return awPlanConfigMapper.insert(awPlanConfig);
    }

    /**
     * 修改启动计划配置
     *
     * @param awPlanConfig 启动计划配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateAwPlanConfig(AwPlanConfig awPlanConfig) throws IOException {
        if (Objects.isNull(awPlanConfig)) {
            throw new ServiceException("修改信息不能为空!") ;
        }
        if (Objects.isNull(awPlanConfig.getId())) {
            throw new ServiceException("修改信息主键不能为空!") ;
        }
        AwStartPlan plan = awStartPlanService.getById(awPlanConfig.getStartPlanId());
        if (plan.getIsDefault().equals(1)) {
            if (StringUtils.isNotBlank(awPlanConfig.getConfig())) {
                AwPlanConfig config = this.getById(awPlanConfig.getId());
                String oldConfig = config.getConfig();
                String newConfig = awPlanConfig.getConfig();
                if (!oldConfig.equals(newConfig)) {
                    StartPlanManager.replaceOldConfig(oldConfig ,newConfig);
                }
            }
        }
        awPlanConfig.setUpdateTime(DateUtils.getNowDate());
        return awPlanConfigMapper.updateById(awPlanConfig);
    }

    /**
     * 批量删除启动计划配置
     *
     * @param ids 需要删除的启动计划配置主键
     * @return 结果
     */
    @Override
    public int deleteAwPlanConfigByIds(Long[] ids)
    {
        return awPlanConfigMapper.deleteAwPlanConfigByIds(ids);
    }

    /**
     * 删除启动计划配置信息
     *
     * @param id 启动计划配置主键
     * @return 结果
     */
    @Override
    public int deleteAwPlanConfigById(Long id)
    {
        return awPlanConfigMapper.deleteAwPlanConfigById(id);
    }


}
